package cc.sunni;

import com.google.common.base.Charsets;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 江黎
 * @since 2022-07-21
 */
@RestController
public class Controller {

    @PostMapping("/downloadQrCode")
    public void downloadQrCode(@RequestParam String content, @RequestParam(required = false) String title,
                               @RequestParam(required = false) String desc, @RequestParam(required = false) String bottom,
                               HttpServletResponse response) throws IOException {
        BufferedImage targetBufferedImage = QrCodeCard.createQrCode(title, desc, content,
                bottom, "templates/logo@2x.png", "templates/announcer.png", 290);
        OutputStream os = response.getOutputStream();
        response.setCharacterEncoding(Charsets.UTF_8.name());
        response.setContentType("multipart/form-data");
        response.setHeader("content-type", "image/png");
        ImageIO.write(targetBufferedImage, "png", os);
    }
}
